//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')
App({
  onLaunch: function () {
    qcloud.setLoginUrl(config.service.loginUrl)
  },
  globalData: {
    host: config.service.host,
    userInfo: {},
    shopInfo: {},
    _shopId: '',
    _goodsId: '',
    cartInfo:[],
    goodsInfo: {},
    inventoryId: {},
    count:0,
    radioValue:'',
    is_authorize_location:1,
    city: '',
    latitude: '',
    longitude:'',
    positioName:'',
    address_latitude: '',
    address_longitude: '',
    address_positioName: '',
    shopcarData: [{//购物车   
      id: '1',
      name: '折后i啊手动阀就是点击发送的金佛啊是是假的佛山折后i啊手动阀就是点击发送的金佛啊是是假的佛山',
      price: '230',
      num: 1,
      types: '白色/39码',
      check: true
    }, {
      id: '2',
      name: '啊哈额和福特好热惊讶所以就如同撒打发士大夫',
      price: '332',
      num: 1,
      types: '粉色/38码'
    }, {
      id: '3',
      name: '啊如何呀还是大范围推广哇额饿啊日hers的说法的事发生的',
      price: '120',
      num: 1,
      types: '白色/41码',
      check: true
    }, {
      id: '4',
      name: '阿桑的歌也会更好的反对犯得上的事发生的',
      price: '320',
      num: 1,
      types: '黑色/37码',
      check: true
    }, {
      id: '5',
      name: '阿桑的歌微软噶士大夫啊士大夫但是飞洒地方士大夫撒',
      price: '630',
      num: 1,
      types: '白色/39码',
      check: true
    }]

  },
  toIndex: function () {
    wx.redirectTo({
      url: '../index/index'
    })
  },
  toOpen:function() {
    wx.redirectTo({
      url: '../open/open'
    })
  },
  toIndexOnline: function () {
    wx.switchTab({
      url: '../index-online/index-online'
    })
  },
  toLogin: function () {
    wx.redirectTo({
      url: '../login/login'
    })
  },
  toCart: function(goodsId) {
    wx.redirectTo({
      url: '../shopping-cart/shopping-cart?goodsId=' + goodsId
    })
  },
  toPay: function (orderId) {
    wx.redirectTo({
      url: '../pay-sucess/pay-sucess?orderId=' + orderId
    })
  },
  toGoodsDetail:function(id) {
    wx.navigateTo({
      url: '../commodity-details/commodity-details?goodsId=' + id
    })
  },
  showToastNone: function (name,time) {
    if(!time){
      time = 500;
    }
    wx.showToast({ title: name, icon: 'none', duration: time })
  },
  changeCart: function (shop_id, session_key,goods_id,type,num) {
    var that = this;
    wx.showLoading({
      title: '处理中',
      duration: 200
    })
    wx.request({
      url: config.service.host + '/cart/changeCart',
      data: {
        'shop_id': shop_id,
        'session_key': session_key,
        'goods_id': goods_id,
        'type': type,
        'num': num,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let data = res.data
        if(data.code == 2000){
        }else{
          wx.showToast({ title: data.msg, icon: 'none', duration: 2000 })
        }
      }
    })
  },
  wxPost: function (url,param) {
    if (!param) param = {};
    return new Promise((resolve, reject) => {
      if (!this.globalData.userInfo.sessionKey) reject('登录超时，请重新登录');
      param['session_key'] = this.globalData.userInfo.sessionKey;
      if (!url) reject('无访问接口');
      wx.request({
        url: url,
        data: param,
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          if (res.data.code == 2000) {
            resolve(res.data.data);
          } else {//返回错误提示信息
            reject(res.data.msg);
          }
        },
        error: function (e) {
          reject('网络出错');
        }
      })
    })
  }
})